<?php

namespace JsonLowCode\Table;

use Closure;
use JsonLowCode\UI;
use JsonLowCode\Form;
use JsonLowCode\Support\Fluent;
use JsonLowCode\Support\Js;
use JsonLowCode\Table;;

class Search extends Fluent
{
    protected Table $table;
    protected array $elements = [];

    protected array $fields = [];
    protected array $fieldTypes = [];
    protected array $data = [];
    protected Form $form;
    protected  $eloquent;

    protected string $bindKey = "";

    protected UI\Button $resetButton;
    protected UI\Button $searchButton;

    public function __construct(Table $table, callable $callback = null){
        parent::__construct();
        $this->table = $table;
        $this->eloquent = $table->eloquent();
        $this->form = Form::make(null,function (Form $form){
            $form->form()->labelPlacement("left")->inline();
        });
        $this->bindKey = $this->form->getBindKey();
        $this->resetButton = UI::button("重置")->onClick($this->table->onReset());
        $this->searchButton =  UI::button("搜索")->primary()->onClick($this->table->onSearch());
        $this->showSearch();
        if ($callback instanceof Closure){
            $this->showSearch();
            call_user_func($callback,$this);
        }else{
            $this->showSearch(false);
        }

    }

    /**
     * 获取搜索内容
     * @return array
     */
    public function getFields(): array
    {
        return $this->fields;
    }

    /**
     * @return string
     */
    public function getBindKey(): string
    {
        return $this->bindKey;
    }

    public function form(): Form
    {
        return $this->form;
    }

    protected function appForm(): UI\AppForm
    {
        return $this->form->form();
    }

    public function data(): array
    {
        return $this->data;
    }

    /**
     * 启用搜索
     * @param bool $showSearch
     * @return $this
     */
    public function showSearch(bool $showSearch = true): static
    {
        $this->table->showSearch($showSearch);
        return $this;
    }

    /**
     * @param $field
     * @param $label
     * @return Search
     */
    public function equal($field, $label): static
    {
        $this->fieldTypes[$field] = "text";
        $this->form->text($field,$label);
        $this->fields[] = [
            'field'=>$field,
            'condition'=>"=",
        ];
        return $this;
    }

    /**
     * @param $field
     * @param $label
     * @return Search
     */
    public function like($field, $label): static
    {
        $this->fieldTypes[$field] = "text";
        $this->form->text($field,$label);
        $this->fields[] =[
            'field'=>$field,
            'condition'=>"like",
        ];
        return $this;
    }



    /**
     * 渲染slotFooter
     * @return void
     */
    protected function renderFooter(): void
    {
        $formItem = new UI\FormField();
        $formItem->children(UI::space([
            $this->resetButton,
            $this->searchButton,
        ]));
        $this->appForm()->slotFooter($formItem);
    }

    protected function render(): void
    {
        $this->table->searchModel(Js::this($this->bindKey), true);
        $this->form->buildField();
        $this->renderFooter();
    }

    public function jsonSerialize(): array
    {
        $this->render();
        return $this->form->jsonSerialize();
    }

}
